package com.yzqp.generator.util.generator.filter.map.add;

import com.yzqp.generator.bean.TableColumnInfo;
import com.yzqp.generator.prop.java.GeneratorBaseProp;
import com.yzqp.generator.util.generator.filter.map.GeneratorMapFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 生成器的 map 的过滤器
 * <p>
 * 添加java所在的包
 */
public class AddJavaPackageListMapFilter implements GeneratorMapFilter {

    GeneratorBaseProp generatorBaseProp;

    private String postfix;

    public AddJavaPackageListMapFilter(String postfix, GeneratorBaseProp generatorBaseProp) {
        this.postfix = postfix;
        this.generatorBaseProp = generatorBaseProp;
    }

    @Override
    public Map<String, Object> filter(Map<String, Object> map, List<TableColumnInfo> tableColumnInfoList) {
        // 根据不同的后缀，返回不同的引入类型
        if ("Bean".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            packageList.add("com.baomidou.mybatisplus.annotation.TableName");
            packageList.add("com.yzqp.common.mvc.base.BaseBean");
            packageList.add("lombok.Data");
            // 添加对应的字段类型的包
            packageList = this.addFieldTypePackage(packageList, tableColumnInfoList);
            map.put("packageList", packageList);
            return map;
        } else if ("Mapper".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            packageList.add("com.yzqp.common.mvc.base.BaseDao");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + "." + generatorBaseProp.getBeanName() + ".*");
            map.put("packageList", packageList);
            return map;
        } else if ("Service".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            packageList.add("com.yzqp.common.mvc.base.BaseService");
            packageList.add("org.springframework.stereotype.Service");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + "." + generatorBaseProp.getBeanName() + ".*");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + "." + generatorBaseProp.getMapperName() + ".*");
            map.put("packageList", packageList);
            return map;
        } else if ("SaveForm".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            packageList.add("io.swagger.annotations.ApiModel");
            packageList.add("io.swagger.annotations.ApiModelProperty");
            packageList.add("lombok.Data");
            packageList.add("javax.validation.constraints.*");
            // 添加对应的字段类型的包
            packageList = this.addFieldTypePackage(packageList, tableColumnInfoList);
            map.put("packageList", packageList);
            return map;
        } else if ("PageForm".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            packageList.add("io.swagger.annotations.*");
            packageList.add("lombok.Data");
            packageList.add("com.yzqp.common.mvc.base.BasePageForm");
            // 添加对应的字段类型的包
            packageList = this.addFieldTypePackage(packageList, tableColumnInfoList);
            map.put("packageList", packageList);
            return map;
        } else if ("Vo".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            packageList.add("lombok.Data");
            // 添加对应的字段类型的包
            packageList = this.addFieldTypePackage(packageList, tableColumnInfoList);
            map.put("packageList", packageList);
            return map;
        } else if ("Controller".equals(postfix)) {
            // 添加包
            List<String> packageList = new ArrayList<>();
            /**
             * import com.yzqp.sys.bean.User;
             * import com.yzqp.sys.form.UserPageForm;
             * import com.yzqp.sys.form.UserSaveForm;
             * import com.yzqp.sys.service.UserService;
             * import com.yzqp.sys.vo.UserVo;
             */
            packageList.add("com.baomidou.mybatisplus.core.metadata.IPage");
            packageList.add("com.yzqp.common.result.*");
            packageList.add("com.yzqp.common.util.BeanUtil");
            packageList.add("io.swagger.annotations.*");
            packageList.add("org.springframework.beans.factory.annotation.Autowired");
            packageList.add("org.springframework.util.StringUtils");
            packageList.add("org.springframework.validation.BindingResult");
            packageList.add("org.springframework.web.bind.annotation.*");
            packageList.add("javax.validation.Valid");
            packageList.add("java.util.List");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + ".bean.*");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + ".service.*");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + ".form.*");
            packageList.add(generatorBaseProp.getBaseJavaPackage() + ".vo.*");
            map.put("packageList", packageList);
            return map;
        }
        return map;
    }

    /**
     * 添加对应字段类型的包
     *
     * @param packageList
     * @param tableColumnInfoList
     */
    private List<String> addFieldTypePackage(List<String> packageList, List<TableColumnInfo> tableColumnInfoList) {
        boolean addDateTypeFlag = false;
        boolean addBigDecimalTypeFlag = false;
        for (TableColumnInfo column : tableColumnInfoList) {
            // 加入日期的包
            if ("Date".equals(column.getJavaType())) {
                if (!addDateTypeFlag) {
                    packageList.add("java.util.Date");
                }
            } else if ("BigDecimal".equals(column.getJavaType())) {
                if (!addBigDecimalTypeFlag) {
                    packageList.add("java.math.BigDecimal");
                }
            }
        }
        return packageList;
    }
}
